package com.lx.day611.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PDFUtils {
    public static String create(JSONArray blockLine,String itineraryNo){//生成pdf
        BaseFont bf;
        Font font = null;
        try {
            String prefixFont = "";
            String os = System.getProperties().getProperty("os.name");
            System.out.println(os);
            if (os.startsWith("win") || os.startsWith("Win")) {
                prefixFont = "C:\\Windows\\Fonts" + File.separator;
            } else {
                prefixFont = "/usr/share/fonts/chinese" + File.separator;
            }

            bf= BaseFont.createFont(prefixFont + "MSYH.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);


//            bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
//                    BaseFont.NOT_EMBEDDED);//创建字体
            font = new Font(bf,12);//使用字体
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document document = new Document();
        try {
            System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
            String os = System.getProperty("os.name");
            if(!os.toLowerCase().startsWith("win")){
                Properties properties = new Properties();
                try {
                    properties.load(PDF2png.class.getResourceAsStream("/config.properties"));
                    PDF2png.PATH=properties.getProperty("path");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else{
                PDF2png.PATH=Thread.currentThread().getContextClassLoader().getResource("").getPath();
            }

            PdfWriter.getInstance(document, new FileOutputStream(PDF2png.PATH+"/"+itineraryNo+".pdf"));
            document.open();


            Paragraph paragraph =new Paragraph("行程号:"+itineraryNo,font);
            //居中显示 0 居左  1 居中 2 居右
            paragraph.setAlignment(2);
            document.add(paragraph);
            float[] widths = {10f,55f,5f,5f,5f,5f};// 设置表格的列宽和列数 默认是4列
            PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
            table.setSpacingBefore(20f);
            table.setWidthPercentage(100);// 设置表格宽度为100%

            PdfPCell cell = null;
            cell = new PdfPCell(new Paragraph("日期",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("行程地区",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("早",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("中",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("晚",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("酒店",font));//
            cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            //以下代码的作用是创建100行数据,其中每行有四列,列依次为 编号 姓名 性别 备注
            JSONObject  curJson=null;
            for (int i = 1; i <=blockLine.size(); i++) {
                curJson=blockLine.getJSONObject(i-1);
                //设置编号单元格
                PdfPCell cell11=new PdfPCell(new Paragraph("第"+i+"天",font));

                JSONArray lineTops = curJson.getJSONArray("lineTops");
                StringBuilder  describ =new StringBuilder ();
                for(int x=0;x<lineTops.size();x++){
                    describ.append(lineTops.getJSONObject(x).getString("activityTime")).append("到达");
                    describ.append(lineTops.getJSONObject(x).getString("scenicName")).append("进行");
                    describ.append(lineTops.getJSONObject(x).getString("describe")).append("游玩 ");
                    describ.append("然后");
                    if(x+1==lineTops.size())
                        describ.append("回酒店");

                }

                PdfPCell cell22=new PdfPCell(new Paragraph(describ.toString(),font));
                PdfPCell cell33=new PdfPCell(new Paragraph((curJson.getString("breakfast").equals("有"))?String.valueOf((char)8730):"X",font));
                PdfPCell cell44=new PdfPCell(new Paragraph((curJson.getString("lunch").equals("有"))?String.valueOf((char)8730):"X",font));
                PdfPCell cell55=new PdfPCell(new Paragraph((curJson.getString("dinner").equals("有"))?String.valueOf((char)8730):"X",font));
                PdfPCell cell66=new PdfPCell(new Paragraph(curJson.getString("hotel"),font));

                //单元格水平对齐方式
                cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
                //单元格垂直对齐方式
                cell11.setVerticalAlignment(Element.ALIGN_CENTER);

                cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell22.setVerticalAlignment(Element.ALIGN_CENTER);

                cell33.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell33.setVerticalAlignment(Element.ALIGN_CENTER);

                cell44.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell44.setVerticalAlignment(Element.ALIGN_CENTER);

                cell55.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell55.setVerticalAlignment(Element.ALIGN_CENTER);


                cell66.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell66.setVerticalAlignment(Element.ALIGN_CENTER);


                table.addCell(cell11);
                table.addCell(cell22);
                table.addCell(cell33);
                table.addCell(cell44);
                table.addCell(cell55);
                table.addCell(cell66);

            }
            document.add(table);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("file create exception");
        }
       return PDF2png.pdf2png(itineraryNo);
    }


    public static void main(String[] args) {

        String jsons="{    \"clientId\": \"YNSGJLXSOC\",    \"dataResult\": [        {            \"groupCode\": \"TES-SSGL-170126T-21\",            \"groupType\": \"省内\",            \"region\": \"山西省\",            \"tripBegin\": \"20170402100000\",            \"tripEnd\": \"20170407173000\",            \"adjustName\": \"云南国旅\",            \"travelRoute\": \"野象谷、国门、勐景来、植物园、傣族园、森林公园、送机\",            \"selfDrive\": \"无\",            \"specialItem\": \"有\",            \"ywProd\": \"国内游方案二\",            \"blockTravel\": [                {                    \"travelName\": \"XXXX 旅行社\",                    \"peopleNumber\": \"4\"                }            ],            \"blockDelegation\": [                {                    \"travelName\": \"XXXX 旅行社\",                    \"peopleNumber\": \"4\"                }            ],            \"blockGuider\": [                {                    \"guideName\": \"和树芳\",                    \"guideCertificate\": \"DD64888L\",                    \"guidePhone\": \"13888778222\",                    \"guideLevel\": \"3\",                    \"companyName\": \"XXX 旅行社\",                    \"startDate\": \"2018-09-14\",                    \"endDate\": \"2018-09-18\"                },                {                    \"guideName\": \"张三\",                    \"guideCertificate\": \"DD64888L\",                    \"guidePhone\": \"13888778222\",                    \"guideLevel\": \"3\",                    \"companyName\": \"XXX 旅行社\",                    \"startDate\": \"2018-09-14\",                    \"endDate\": \"2018-09-18\"                }            ],            \"blockCars\": [                {                    \"carPlate\": \"云 AXXXXX\",                    \"plateColor\": \"蓝\",                    \"driverName\": \"李斯\",                    \"driverPhone\": \"13887766111\",                    \"companyName\": \"XXX 运输公司\",                    \"startDate\": \"2018-09-14\",                    \"endDate\": \"2018-09-18\"                },                {                    \"carPlate\": \"云 AXXXXX\",                    \"plateColor\": \"蓝\",                    \"driverName\": \"李斯七\",                    \"driverPhone\": \"13887766111\",                    \"companyName\": \"XXX 运输公司\",                    \"startDate\": \"2018-09-18\",                    \"endDate\": \"2018-09-19\"                }            ],            \"blockLine\": [                {                    \"dinner\": \"有\",                    \"lineDesp\": \"西双版纳野象谷\",                    \"breakfast\": \"有\",                    \"lineDate\": \"1\",                    \"hotel\": \"\",                    \"lunch\": \"有\",                    \"lineTops\": [                        {                            \"istop\": \"1\",                            \"activityTime\": \"09:15:00\",                            \"arrivalTime\": \"60\",                            \"gotoCityName\": \"大理\",                            \"scenicName\": \"大理古城\",                            \"scenicAddress\": \"大理市古城路 XX 号\",                            \"scenicPoint\": \"\",                            \"describe\": \"BUS 大理，白族民居，手工制作扎染，三道茶歌舞表演，花语牧场\"                        },                        {                            \"istop\": \"2\",                            \"activityTime\": \"18:15:00\",                            \"arrivalTime\": \"下关\",                            \"gotoCityName\": \"大理\",                            \"scenicName\": \"下关\",                            \"scenicAddress\": \"大理时下关镇 XX 号\",                            \"scenicPoint\": \"\",                            \"describe\": \"白族民居，歌舞表演，品尝美食\"                        }                    ]                },                {                    \"dinner\": \"有\",                    \"lineDesp\": \"缅甸国门参观浏览\",                    \"breakfast\": \"有\",                    \"lineDate\": \"2\",                    \"hotel\": \"\",                    \"lunch\": \"有\",                    \"lineTops\": [                        {                            \"istop\": \"1\",                            \"activityTime\": \"09:15:00\",                            \"arrivalTime\": \"60\",                            \"gotoCityName\": \"大理\",                            \"scenicName\": \"大理古城\",                            \"scenicAddress\": \"大理市古城路 XX 号\",                            \"scenicPoint\": \"\",                            \"describe\": \"BUS 大理，白族民居，手工制作扎染，三道茶歌舞表演，花语牧场\"                        },                        {                            \"istop\": \"2\",                            \"activityTime\": \"18:15:00\",                            \"arrivalTime\": \"下关\",                            \"gotoCityName\": \"大理\",                            \"scenicName\": \"下关\",                            \"scenicAddress\": \"大理时下关镇 XX 号\",                            \"scenicPoint\": \"\",                            \"describe\": \"白族民居，歌舞表演，品尝美食\"                        }                    ]                }            ],            \"blockTrourist\": [                {                    \"touName\": \"骏捷\",                    \"touSex\": \"女\",                    \"certifiName\": \"居民户口簿\",                    \"cerfifiNum\": \"530103199704170018\",                    \"ywOdNum\": 1                },                {                    \"touName\": \"萌萌\",                    \"touSex\": \"女\",                    \"certifiName\": \"居民户口簿\",                    \"cerfifiNum\": \"530103199704170018\"                },                {                    \"touName\": \"王丽\",                    \"touSex\": \"女\",                    \"certifiName\": \"居民户口簿\",                    \"cerfifiNum\": \"530103199704170018\",                    \"ywOdNum\": 1                },                {                    \"touName\": \"join\",                    \"touSex\": \"女\",                    \"certifiName\": \"居民户口簿\",                    \"cerfifiNum\": \"530103199704170018\",                    \"ywOdNum\": 1                }            ],            \"blockLeader\": {                \"leader\": \"王云\",                \"leaderMobile\": \"13589012345\",                \"beginFlightTime\": \"2017-06-30 11:19:00\",                \"beginFlightNumber\": \"GH0012\",                \"collectionTime\": \"2017-06-30 11:05:00\",                \"collectionLocations\": \"白云机场\"            }        }    ]}";
        System.out.println(jsons);
        JSONArray blockLine = JSON.parseObject(jsons).getJSONArray("dataResult").getJSONObject(0).getJSONArray("blockLine");
        create(blockLine,"TES-SSGL-170126T-21");
    }
}
